package org.dormi.learn.definition;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;
import java.util.UUID;

/**
 * @author : dormi330
 * @since : 2021/1/27/0027
 * 顶点
 */
public class Vertex {
    public static final int SEQ_NOT_SET = -1;

    private static final Logger log = LoggerFactory.getLogger(Vertex.class);

    public final String uid;
    private final String random = UUID.randomUUID().toString();

    /**
     * Arrival Time is the time at which the vertex was explored for the first time in the DFS
     * Departure Time is the time at which we have explored all the neighbors of the vertex and we are ready to backtrack
     */
    private int departure = SEQ_NOT_SET;
    private int arrival = SEQ_NOT_SET;

    public Vertex(String uid) {
        this.uid = uid;
    }

    @Override
    public String toString() {
        return String.format("V[%s]", uid);
    }

    @Override
    public int hashCode() {
        return uid.hashCode();
    }

    @Override
    public boolean equals(Object other) {
        if (other == null) {
            return false;
        }

        if (!(other instanceof Vertex)) {
            return false;
        }

        Vertex v2 = (Vertex) other;
        if (Objects.equals(uid, v2.uid)) {
            return true;
        }

        return false;
    }

    public int getArrival() {
        return arrival;
    }

    public int getDeparture() {
        return departure;
    }

    public int arrival(int seq) {
        if (this.arrival == SEQ_NOT_SET) {
            log.debug("arrival [uid={}, {} -> {}]", uid, this.arrival, seq);
            this.arrival = seq;
        }
        return this.arrival;
    }

    public int departure(int seq) {
        if (this.departure == SEQ_NOT_SET) {
            log.debug("departure [uid={}, {} -> {}]", uid, this.departure, seq);
            this.departure = seq;
        }
        return this.departure;
    }
}
